﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AdmBase;
using Guedes_Library;

public partial class admin_pages_newsletter_Novo : TelaBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        IsLogado();
        string id = Request.QueryString["id"];

        if(id == null)
              Response.Redirect("~/admin/pages/newsletter/Default.aspx");

        if (id != null && !IsPostBack)
        {
            news_lt n = NBusiness.GetById(int.Parse(id));
            if (n == null)
                Response.Redirect("~/admin/pages/newsletter/Default.aspx");
            txtNome.Text = n.Name;
            txtEmail.Text = n.e_mail;
            hdId.Value = n.ID.ToString();
        }

    }

    private NewsletterBusiness nBusiness;

    public NewsletterBusiness NBusiness
    {
        get
        {
            if (nBusiness == null)
                nBusiness = new NewsletterBusiness();
            return nBusiness;
        }
        set { nBusiness = value; }
    }

    protected void Save(object sender, EventArgs e)
    {
        Retorno retorno;
        news_lt n = new news_lt();
        if (hdId.Value != "")
            n.ID = int.Parse(hdId.Value);
        n.Name = txtNome.Text;
        n.e_mail = txtEmail.Text;
        retorno = NBusiness.Salvar(n);

        if (retorno.Error)
            ShowMensagem("Atenção", retorno.Mensagem);
        else
        {
            Session["msg"] = retorno.Mensagem;
            Response.Redirect("~/admin/pages/newsletter/Default.aspx");
        }
    }

    public void LimpaCampo()
    {
        txtNome.Text = String.Empty;
        txtEmail.Text = String.Empty;
        hdId.Value = String.Empty;
    }
}